﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

namespace HBKY.Shop.Common
{
   public static class StringHelper
    {

        private static bool _windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

        /// <summary>
        /// 字符截取
        /// </summary>
        public static List<string> SplitToList(this string str)
        {
            if (string.IsNullOrWhiteSpace(str)) return new List<string>();
            return str.Split(',').ToList();
        }

        public static string ReplacePath(this string path)
        {
            if (string.IsNullOrEmpty(path))
                return "";
            if (_windows)
                return path.Replace("/", "\\");
            return path.Replace("\\", "/");

        }

        /// <summary>
        /// 去除转移字符
        /// </summary>
        public static string RemoveTransferCharacter(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
                return string.Empty;
            return Regex.Replace(str, @"\s", string.Empty);
        }


        /// <summary>
        /// 动态替换模板
        /// </summary>
        public static string TempleteReplaces(this string content, Dictionary<string, string> dicParams)
        {
            //首先找到所有需要执行替换的字段
            if (dicParams == null || string.IsNullOrWhiteSpace(content)) return content;

            foreach (var item in dicParams)
            {
                content = content.Replace($"{{{item.Key}}}", $"{item.Value}");
            }
            return content;
        }

        /// <summary>
        /// 根据分隔符返回前n条数据
        /// </summary>
        /// <param name="content">数据内容</param>
        /// <param name="separator">分隔符</param>
        /// <param name="top">前n条</param>
        /// <param name="isDesc">是否倒序（默认false）</param>
        /// <returns></returns>
        public static List<string> GetTopDataBySeparator(string content, string separator, int top, bool isDesc = false)
        {
            if (string.IsNullOrEmpty(content))
            {
                return new List<string>() { };
            }

            if (string.IsNullOrEmpty(separator))
            {
                throw new ArgumentException("message", nameof(separator));
            }

            var dataArray = content.Split(separator).Where(d => !string.IsNullOrEmpty(d)).ToArray();
            if (isDesc)
            {
                Array.Reverse(dataArray);
            }

            if (top > 0)
            {
                dataArray = dataArray.Take(top).ToArray();
            }

            return dataArray.ToList();
        }


        #region string
        /// <summary>
        /// 判断字段不为空
        /// [null,""," "]
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }

        /// <summary>
        /// 判断字段不为空
        /// [null,""]
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        #endregion

        /// <summary>
        /// 返回guid 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string AddGuid(this string str)
        { 
           return str.IsNullOrWhiteSpace() ? Guid.NewGuid().ToString() : str;
        }

        /// <summary>
        /// 返回Guid
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GenerateGuid => Guid.NewGuid().ToString();

        /// <summary>
        /// StringFormat
        /// </summary>
        /// <param name="str"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        public static string StringFormat(this string str, params string [] items)
        {
            return string.Format(str, items);
        }
    }
}
